<template>
  <el-aside class="nav-aside" width="200px">
    <!-- 顶部Logo -->
    <div class="nav-header">
      <div class="logo">
        <el-icon><Document /></el-icon>
        <span>Note</span>
      </div>
    </div>

    <!-- 菜单 -->
    <div class="nav-menu">
      <el-menu default-active="1" class="el-menu-vertical">
        <el-menu-item index="1" @click="router.push('/')">
          <el-icon><HomeFilled /></el-icon>
          <span>开始</span>
        </el-menu-item>
        <el-menu-item index="2" @click="router.push('/aichat/')">
          <el-icon><EditPen /></el-icon>
          <span>AI 写作</span>
        </el-menu-item> 
        <el-menu-item index="3">
          <el-icon><Bell /></el-icon>
          <span>通知</span>
        </el-menu-item>
      </el-menu>
      <div class="aside-bottom">
        <el-divider />
        <el-popover
          :width="200"
          trigger="hover"
          popper-class="user-popover"
          placement="top"
        >
          <template #reference>
            <div class="user-info">
              <el-avatar :size="32" :src="userInfo.avatar" />
              <span class="username">{{ userInfo.name }}</span>
              <el-icon class="expand-icon"><CaretTop /></el-icon>
            </div>
          </template>
          <div class="user-menu">
            <button class="menu-item" @click="handleSetting">
              <el-icon><Setting /></el-icon>
              <span>设置</span>
            </button>
            <el-divider />
            <button class="menu-item logout" @click="handleLogout">
              <el-icon><SwitchButton /></el-icon>
              <span>退出登录</span>
            </button>
          </div>
        </el-popover>
      </div>
    </div>
  </el-aside>
</template>

<script setup>
import { Document, HomeFilled, EditPen, Bell, CaretTop, Setting, SwitchButton } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { ref } from 'vue'
import { useUserStore } from '@/store/user'

const router = useRouter()
const store = useUserStore()

// 模拟用户信息
const userInfo = ref({
  name: 'Joey',
  avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
})

// 处理设置点击
const handleSetting = () => {
  console.log('打开设置')
}

// 处理退出登录
const handleLogout = () => {
  if (confirm('确定要退出登录吗？')) {
    router.push('/auth')}
  store.fullLogout()
}
</script>

<style scoped>
.nav-aside {
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  background: #fff;
  border-right: 1px solid #eee;
  display: flex;
  flex-direction: column;
}

.nav-header {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  border-bottom: 1px solid #eee;
}

.logo {
  display: flex;
  align-items: center;
  font-size: 1.5rem;
  font-weight: bold;
  color: #409EFF;
  gap: 8px;
}

.nav-menu {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.el-menu {
  border-right: none;
}

.aside-bottom {
  margin-top: auto;
  padding-bottom: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  padding: 12px 20px;
  cursor: pointer;
  transition: all 0.3s ease;

  &:hover {
    background: #f6f6f6;
  }

  .username {
    margin-left: 12px;
    flex: 1;
    font-size: 14px;
    color: #333;
  }

  .expand-icon {
    color: #909399;
    font-size: 12px;
    transition: transform 0.3s ease;
  }

  &:hover .expand-icon {
    transform: rotate(180deg);
  }
}

:deep(.user-popover) {
  padding: 8px 0;
  
  .user-menu {
    .menu-item {
      display: flex;
      align-items: center;
      padding: 8px 16px;
      cursor: pointer;
      transition: all 0.3s ease;
      color: #606266;

      &:hover {
        background: #f5f7fa;
      }

      .el-icon {
        margin-right: 8px;
        font-size: 16px;
      }

      &.logout {
        color: #f56c6c;
      }
    }
  }
}
</style>
